package dataStruct;

import java.awt.Color;
import java.util.ArrayList;

public class GraphView extends javax.swing.JPanel {
	
	Graph<? extends Displayable> graph;
	
	public GraphView(Graph<? extends Displayable> graph)
	{
		this.graph = graph;
	}
	
	@Override
	protected void paintComponent(java.awt.Graphics g)
	{
		super.paintComponent(g);
		
		java.util.List<? extends Displayable> vertices = 
				graph.getVertices();
		//draw vertices
		for(int i=0; i<vertices.size(); ++i)
		{
			int x = vertices.get(i).getX();
			int y = vertices.get(i).getY();
			String name = vertices.get(i).getName();
			g.setColor(Color.BLACK);
			g.fillOval(x-8, y-8, 16, 16);
			g.setColor(Color.RED);
			g.drawString(name, x-12, y-12);
		}
		
		//draw edges
		for(int i=0; i<graph.getSize(); ++i)
		{
			java.util.List<Integer> neighbors = graph.getNeighbors(i);
			for(int j=0; j<neighbors.size(); ++j)
			{
				int v = neighbors.get(j);
				int x1 = graph.getVertex(i).getX();
				int y1 = graph.getVertex(i).getY();
				int x2 = graph.getVertex(v).getX();
				int y2 = graph.getVertex(v).getY();
				g.setColor(Color.BLACK);
				g.drawLine(x1, y1, x2, y2);
			}
		}
		
	}
}
